package com.bing.mvc.validator;

import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by kaibb on 2014/10/4.
 */
public class ForbiddenValidator implements ConstraintValidator<Forbidden,String> {
    private String[] forbiddenWords = {"admin"};
    @Override
    public void initialize(Forbidden constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(StringUtils.isEmpty(value)) {
            return true;
        }

        for(String word : forbiddenWords) {
            if(value.contains(word)) {
                return false;//验证失败
            }
        }
        return true;
    }

}
